package com.aaa.tars.controller;

import com.aaa.tars.entity.Notes;
import com.aaa.tars.service.NotesService;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @FileName: NotesController
 * @Description: 游记控制层
 * @Author: 李宗盛
 * @CreateTime: 2025/9/12 9:30
 * @Version: 17
 */
@RestController
@RequestMapping("/notes")
public class NotesController {
    @Autowired
    private NotesService notesService;
    
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Notes> page) {
        return ResultUtil.success(notesService.queryByPage(page));
    }
    
    @GetMapping("queryById")
    public Result queryById(Integer noteId) {
        return ResultUtil.success(notesService.queryById(noteId));
    }

    // NotesController.java
    @PostMapping("add")
    public Result add(@RequestBody Notes notes) {
        // 设置创建时间
        notes.setCreateTime(new Date());

        // 保存游记
        int result = notesService.add(notes);

        if (result > 0) {
            // 返回新生成的主键ID，而不是影响行数
            return ResultUtil.success(notes.getNoteId());
        } else {
            return ResultUtil.error("保存失败");
        }
    }
    
    @PostMapping("update")
    public Result update(@RequestBody Notes notes) {
        return ResultUtil.success(notesService.update(notes));
    }
    
    @DeleteMapping("deleteById")
    public Result deleteById(Integer noteId) {
        return ResultUtil.success(notesService.deleteById(noteId));
    }
}